/**
 * @description 商品列表增删改查 api 路由
 * @author LI
 */

const router = require('koa-router')()
const {
  getGoodsAll,
  addGoods,
  updateGood,
  deleteGood
} = require('../../controller/good')

router.prefix('/jihuide/good')

// 添加商品信息
router.post('/addGoods', async (ctx, next) => {
  let { goodSn, goodName, goodType, goodSort, goodImg, goodVideo, remark } = ctx.request.body
  goodSort = parseInt(goodSort)
  // 接下来交给控制层
  ctx.body = await addGoods({
    goodSn,
    goodName,
    goodType,
    goodSort,
    goodImg,
    goodVideo,
    remark
  })
})

// 查询所有账号信息
router.post('/getGoodsAll', async (ctx, next) => {
  const { goodType, goodName, page, limit } = ctx.request.body
  // 接下来交给控制层
  ctx.body = await getGoodsAll({
    goodType,
    goodName,
    page,
    limit,
  })
})

// 修改商品
router.post('/updateGood', async (ctx, next) => {
  let { id, goodSn, goodName, goodType, goodSort, goodImg, goodVideo, remark } = ctx.request.body
  id = parseInt(id)
  goodSort = parseInt(goodSort)
  // 接下来交给控制层
  ctx.body = await updateGood({
    id,
    goodSn,
    goodName,
    goodType,
    goodSort,
    goodImg,
    goodVideo,
    remark
  })
})

// 删除商品信息
router.post('/deleteGood', async (ctx, next) => {
  let { id } = ctx.request.body
  id = parseInt(id)
  // 接下来交给控制层
  ctx.body = await deleteGood(id)
})

module.exports = router
